<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" th:href="@{lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
        .hide{
            display: none;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn" id="btn-add">添加一级菜单</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a  name="{{d.pid}}"  class="layui-btn layui-btn-xs"    lay-event="add" {{d.pid!=-1 ? 'style="display: none;"' : ''  }} >新增</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" {{d.pid!=-1 ? 'style="width: 92px;"' : ''  }}>修改</a>
</script>

<script type="text/html" id="menu-status">
    <input type="checkbox"  name="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="开|关"
           lay-filter="onOff" {{ d.status== 1 ? 'checked' : '' }}>
</script>

<script th:src="@{lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',//菜单对象的ID
            treePidName: 'pId',//父级菜单的ID
            elem: '#munu-table',
            url: '/getMenuInfo',//数据来源
            page: false,
            cols: [
                [
                    {type: 'numbers'},
                    {field: 'title', minWidth: 200, title: '菜单名称'},
                    {field: 'href', title: '菜单url'},
                    {field: 'status', width: 80, templet: "#menu-status", align: 'center', title: '状态'},
                    {
                        field: 'pId', width: 100, align: 'center', templet: function (d) {
                            if (d.pid == -1) {
                                return '<span class="layui-badge-rim">一级菜单</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-blue">二级菜单</span>';
                            }
                        }, title: '类型'
                    },
                    {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
                ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $('#btn-add').click(function () {
            var index = layer.open({
                title: "添加菜单",//弹出层的标题
                type: 2,
                shade: 0.2,//遮罩层的透明度
                maxmin: true,//是否显示最大化和最小化按钮
                shadeClose: true,//是否支持点击遮罩层 关闭弹出层
                area: ["50%", "70%"],//弹出层的大小  （宽高）
                //表格的URL
                content: "edit_menu_page?&pageType=1",
            });

            $(window).on("resize", function () {
                layer.full(index);
            });
        })


        form.on('switch(onOff)', function (obj) {

            // var status=this.value;
            var status = 0;
            var id = this.name;
            
            if (obj.elem.checked) {
                status = 1;
            } else {
                status = 0;
            }
            $.ajax({
                url: "/delMenu",
                type: "get",
                data: {menuId: id,status:status},
                success: function (res) {
                    console.log("pid="+res.data);
                    if (res.code == "0000") {
                        layer.msg("操作成功",function () {
                            // window.location.reload();
                            layui.jquery('input[name="'+res.data+'"]')
                                .attr('checked', 'checked')
                                .next().addClass('layui-form-onswitch')
                                .children("em").html("开");;  //改变开关为 开
                            // layui.jquery('input[name="'+res.data+'"]').next().addClass('layui-form-onswitch').children("em").html("开"); //开
                        });

                    } else {

                        layer.msg(res.msg,function () {
                            // window.location.reload();
                            console.log(status+"__"+id);

                            // form.render('checkbox');
                            if (status==1){
                                layui.jquery('input[name="'+id+'"]').removeAttr('checked');  //改变开关为 关
                                form.render('checkbox');
                            }else {
                                layui.jquery('input[name="'+id+'"]').attr('checked','checked');
                                layui.jquery('input[name="'+id+'"]').next().addClass('layui-form-onswitch').children("em").html("开"); //开
                            }

                        });


                    }
                }
            })

        })
        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            var pageType=3;
           if (layEvent === 'edit') {

               var pId=data.pid;
               if (pId==-1){
                    pageType=2;//修改一级菜单
               }

               var index = layer.open({
                   title: "添加菜单",//弹出层的标题
                   type: 2,
                   shade: 0.2,//遮罩层的透明度
                   maxmin: true,//是否显示最大化和最小化按钮
                   shadeClose: true,//是否支持点击遮罩层 关闭弹出层
                   area: ["50%", "70%"],//弹出层的大小  （宽高）
                   //表格的URL
                   content: "/edit_menu_page?id="+data.id+"&pageType="+pageType,
               });

               $(window).on("resize", function () {
                   layer.full(index);

               });
            } else if (layEvent === 'add') {

               var index = layer.open({
                   title: "添加菜单",//弹出层的标题
                   type: 2,
                   shade: 0.2,//遮罩层的透明度
                   maxmin: true,//是否显示最大化和最小化按钮
                   shadeClose: true,//是否支持点击遮罩层 关闭弹出层
                   area: ["50%", "70%"],//弹出层的大小  （宽高）
                   //表格的URL
                   content: "/edit_menu_page?id="+data.id+"&pageType=1",
               });

               $(window).on("resize", function () {
                   layer.full(index);

               });
            }
        });
    });
</script>
</body>
</html>